package anc.server.tankshooter.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import anc.server.tankshooter.App;
import anc.server.tankshooter.models.Account;
import anc.server.tankshooter.models.IEntity;

/**
 * Quản lý tài khoản - Table : account
 * @author tauitdnmd
 *
 */
public class AccountManager extends IManager{

	public AccountManager(Connection connection) {
		super(connection,Connect.ACCOUNT_TABLE);
	}


	/**
	 * @param account
	 * @return Trả về tài khoản. null nếu thất bại
	 */
	public Account get(int accountId) {
		
		Account account = null;
		try{
			String sql ="SELECT * FROM "+getTableName()+" WHERE account_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, accountId);
			ResultSet reader = statement.executeQuery();
			if(reader!=null && reader.next()){
				account = new Account();
				account.setAccountId(reader.getInt(1));
				account.setUsername(reader.getString(2));
				account.setPassword(reader.getString(3));
				account.setAvatarId(reader.getInt(4));
				account.setName(reader.getString(5));
				account.setEmail(reader.getString(6));
				account.setLevel(reader.getInt(7));
				reader.close();
			}
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return account;
	}
	
	/**
	 * @param account
	 * @return Trả về tài khoản. null nếu thất bại
	 */
	public Account get(String username, String password) {
		
		Account account = null;
		try{
			String sql ="SELECT * FROM "+getTableName()+" WHERE username=? AND password=MD5(?)";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setString(1, username);
			statement.setString(2, password);
			ResultSet reader = statement.executeQuery();
			if(reader!=null && reader.next()){
				account = new Account();
				account.setAccountId(reader.getInt(1));
				account.setUsername(reader.getString(2));
				account.setPassword(reader.getString(3));
				account.setAvatarId(reader.getInt(4));
				account.setName(reader.getString(5));
				account.setEmail(reader.getString(6));
				account.setLevel(reader.getInt(7));
				reader.close();
			}
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return account;
	}
	
	/**
	 * Thêm tài khoản mới
	 * @param account
	 * @return Trả về id của tài khoản mơi đc thêm vào. -1 nếu thất bại
	 */
	@Override
	public int add(IEntity entity) {
		
		int newId = -1;
		try{
			Account account = (Account)entity;
			String sql ="INSERT INTO "+getTableName()+" VALUES(null,?,MD5(?),?,?,?,?)";
			PreparedStatement statement = mConnection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			statement.setString(1, account.getUsername());
			statement.setString(2, account.getPassword());
			statement.setInt(3, account.getAvatarId());
			statement.setString(4, account.getName());
			statement.setString(5, account.getEmail());
			statement.setInt(6, account.getLevel());
			statement.execute();
			if(statement.getUpdateCount()>0){
				ResultSet reader = statement.getGeneratedKeys();
				if(reader.next()){
					newId = reader.getInt(1);
				}
				reader.close();
			}
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return newId;
	}

	/**
	 * Cập nhật thông tin mới cho tài khoản
	 * Chỉ có mã tài khoản là không thể thay đổi
	 * @param account
	 * @return
	 */
	@Override
	public boolean update(IEntity entity) {
		boolean isSuccess = false;
		try{
			Account account = (Account)entity;
			String sql ="UPDATE "+getTableName()+
						" SET username=?,password=MD5(?),avatar_id=?,name=?,email=?,level=?"+
						" WHERE account_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setString(1, account.getUsername());
			statement.setString(2, account.getPassword());
			statement.setInt(3, account.getAvatarId());
			statement.setString(4, account.getName());
			statement.setString(5, account.getEmail());
			statement.setInt(6, account.getLevel());
			statement.setInt(7, account.getAccountId());
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	/**
	 * Xóa 1 tài khoản dựa theo mã tài khoản
	 * @param accountId
	 * @return
	 */
	@Override
	public boolean delete(int accountId) {
		boolean isSuccess = false;
		try{
			String sql ="DELETE FROM "+getTableName()+" WHERE account_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, accountId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	/**
	 * Kiểm tra xem mã tài khoản này có tồn
	 * tại trong database hay chưa
	 * @param account
	 * @return 
	 */
	@Override
	public boolean isExist(int accountId) {
		boolean isSuccess = false;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName()+" WHERE account_id=?";
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.setInt(1, accountId);
			statement.execute();
			isSuccess = statement.getUpdateCount()>0;
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return isSuccess;
	}

	/**
	 * Đếm số lượng tài khoản 
	 */
	@Override
	public int count() {
		int counter = 0;
		try{
			String sql ="SELECT COUNT(*) FROM "+getTableName();
			PreparedStatement statement = mConnection.prepareStatement(sql);
			statement.execute();
			counter = statement.getUpdateCount();
			statement.close();
		}catch(Exception ex){
			App.printError(ex);
		}
		return counter;
	}
}
